package com.codenjoy.calculator;

import com.codenjoy.calculator.base.Base;
import com.codenjoy.calculator.base.BaseFactory;

public class Calculator {

    public String calculate(String expr, String bs) {
        Base base = BaseFactory.getBase(bs);

        int pos = expr.indexOf("+");
        if (pos <= 0 || pos == expr.length() || expr.split("[+]").length != 2) {
            throw new IllegalArgumentException("Invalid expression format");
        }
        for (int i = 0; i < expr.length(); i++) {
            char tempSymbol = expr.charAt(i);
            if (tempSymbol == '+') {
                continue;
            }
            base.validateSymbol(tempSymbol);
        }

        long firstNumbInDecimal = base.formatFromBaseToDecimal(expr.substring(0, pos));
        long secondNumbInDecimal = base.formatFromBaseToDecimal(expr.substring(pos + 1, expr.length()));


        long result = firstNumbInDecimal + secondNumbInDecimal;

        return base.formatFromDecimalToBase(result);
    }




}
